package com.ossjk.api.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.oa.accounting.entity.CwCapitals;
import com.ossjk.oa.accounting.entity.CwSalary;
import com.ossjk.oa.accounting.service.ICwCapitalsService;
import com.ossjk.oa.accounting.service.ICwSalaryService;
import com.ossjk.oa.attendance.vo.SalaryAttVo;
import com.ossjk.oa.finance.entity.Salary;
import com.ossjk.oa.finance.service.ISalaryService;
import com.ossjk.oa.paid.entity.Paid;
import com.ossjk.oa.paid.service.IPaidService;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.entity.Post;
import com.ossjk.oa.system.service.IDepartmentService;
import com.ossjk.oa.system.service.IEmployeePostService;
import com.ossjk.oa.system.service.IEmployeeService;
import com.ossjk.oa.system.service.IPostService;
import com.ossjk.oa.system.vo.EmpDeptVo;
import com.ossjk.oa.system.vo.EmpVo;
import com.ossjk.oa.system.vo.EmployeeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/Wages")
@Api(description = "工资月报")
public class WagesController extends BaseController {

    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private ICwSalaryService iCwSalaryService;
    @Autowired
    private IDepartmentService iDepartmentService;
    @Autowired
    private ICwCapitalsService iCwCapitalsService;

    /**
     * 陈锐彬
     * 10.1 按月查看部门所有人的工资表    （需要移动API）
     */
    @GetMapping("selectAll")
    @ApiOperation(value = "查看部门所有人的工资表")
    @ApiImplicitParam(name = "month", paramType = "query", required = true, dataType = "String", value = "月份")
    public ResponseBean<List<CwSalary>> selectAll(String month, String did) {
        //当month为空时，默认查上个月
        if (CommonUtil.isBlank(month)) {
            Calendar now = Calendar.getInstance();
            now.add(Calendar.MONTH, -1);
        }
        //获取登录用户信息
        String UserId = getCurrnetUserId();
        Wrapper wrapper = Condition.create().eq("id", UserId);
        List<EmpDeptVo> empDeptVos = iEmployeeService.selectEmpDept(wrapper);
        EmpDeptVo empDeptVo = empDeptVos.get(0);
        Wrapper wrapper1 = Condition.create().like("date", month);
        Wrapper wrapper2 = Condition.create();
        if (empDeptVo.getLevel() == 1) {
            List<EmpVo> empVos = iEmployeeService.selectEmpVosByDid(did);
            List list = new ArrayList();
            for (EmpVo empVo : empVos) {
                list.add(empVo.getId());
            }
            Wrapper wrapper3 = Condition.create().in("empid", list);
            List list1 = iCwSalaryService.selectList(wrapper3);
            return (ResponseBean<List<CwSalary>>) responseSuccess(list1);
        } else {
            return (ResponseBean<List<CwSalary>>) responseFail("您暂没有该权限");
        }
    }


    /**
     * 部门列表
     */
    @GetMapping("DepartList")
    @ApiOperation(value = "部门列表")
    public ResponseBean<List<Department>> DepartList() {
        Wrapper wrapper = Condition.create();
        List department = iDepartmentService.selectName(wrapper);
        return (ResponseBean<List<Department>>) responseSuccess(department);
    }

    /**
     * 10.2 人工资表操作			cw_salary
     * 财务-确认功能
     * 总经理-审批功能   ---审批意见--通过、不通过      通过之后就可以查看数据
     * 出纳-发放管理  （同时写入记账流水cw_capitals）发放时间
     */
    //工资状态：1：待确认  2：财务-确认功能 3：财务-不允通过 4：总经理审批通过5：总经理审批不通过 6：出纳发放
    @GetMapping("ApprovalSome")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "did", paramType = "query", dataType = "String", required = true, value = "部门id"),
            @ApiImplicitParam(name = "month", paramType = "query", dataType = "String", required = true, value = "月份")
    })
    @ApiOperation(value = "工资表操作")
    public ResponseBean<List<CwSalary>> Approval(String did, String month) {
        //获取登录用户信息
        String UserId = getCurrnetUserId();
        Wrapper wrapper = Condition.create().eq("id", UserId);
        List<EmpDeptVo> empDeptVos = iEmployeeService.selectEmpDept(wrapper);
        EmpDeptVo empDeptVo = empDeptVos.get(0);
        Wrapper wrapper1 = Condition.create();
        System.out.println(empDeptVo.getLevel());
        List list = null;
        if (empDeptVo.getLevel() == 3) {//按部门返回月工资表             财务-确认功能
            List<EmpVo> empVos = iEmployeeService.selectEmpVosByDid(did);
            List emp = new ArrayList();
            for (EmpVo empVo : empVos) {
                emp.add(empVo.getId());
            }
            wrapper1.eq("states", 1).like("wagemonth", month).in("empid", emp);
            list = iCwSalaryService.selectList(wrapper1);
        } else if (empDeptVo.getLevel() == 1) {//总经理-审批功能           审批的是财务已确认工资表
            List<EmpVo> empVos = iEmployeeService.selectEmpVosByDid(did);
            List emp = new ArrayList();
            for (EmpVo empVo : empVos) {
                emp.add(empVo.getId());
            }
            wrapper1.eq("states", 2).like("wagemonth", month).in("empid", emp);
            list = iCwSalaryService.selectList(wrapper1);
        } else if (empDeptVo.getLevel() == 2) {//出纳-发放管理           发放在总经理审批通过的前提下
            List<EmpVo> empVos = iEmployeeService.selectEmpVosByDid(did);
            List emp = new ArrayList();
            for (EmpVo empVo : empVos) {
                emp.add(empVo.getId());
            }
            wrapper1.eq("states", 4).like("wagemonth", month).in("empid", emp);
            list = iCwSalaryService.selectList(wrapper1);
        }
        return (ResponseBean<List<CwSalary>>) responseSuccess(list);
    }

    /**
     * 审批某个员工
     * stat:
     * 2:财务确认      3:财务不允通过    4:总经理审批通过   5:总经理审批不通过  6:出纳发放
     *
     * @param id
     * @return
     */
    @GetMapping("ApprovalOne")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", dataType = "String", required = true, value = "工资表id"),
            @ApiImplicitParam(name = "confirm", paramType = "query", dataType = "Integer", required = true, value = "执行操作状态码")
    })
    @ApiOperation(value = "审批过程")
    public ResponseBean ApprovalOne(String id, Integer confirm) {
        //获取登录用户信息
        String UserId = getCurrnetUserId();
        Wrapper wrapper = Condition.create().eq("id", UserId);
        List<EmpDeptVo> empDeptVos = iEmployeeService.selectEmpDept(wrapper);
        EmpDeptVo empDeptVo = empDeptVos.get(0);
        Wrapper wrapper1 = Condition.create();
        System.out.println(empDeptVo.getLevel());
        Wrapper wrapper2 = Condition.create();
        System.out.println(confirm);
        if (empDeptVo.getLevel() == 3) {
            if (confirm == 2) {
                wrapper2.eq("id", id).eq("states", 1);
                CwSalary cwSalary = iCwSalaryService.selectOne(wrapper2);
                System.out.println(cwSalary);
                cwSalary.setStates(2);//财务确认
                iCwSalaryService.updateById(cwSalary);
                return (ResponseBean) responseSuccess("已确认");
            }
            if (confirm == 3) {
                wrapper2.eq("id", id).eq("states", 1);
                CwSalary cwSalary = iCwSalaryService.selectOne(wrapper2);
                cwSalary.setStates(3);//财务不允通过
                iCwSalaryService.updateById(cwSalary);
                return (ResponseBean) responseSuccess("不允通过");
            }

        } else if (empDeptVo.getLevel() == 1) {
            if (confirm == 4) {
                wrapper2.eq("id", id).eq("states", 2);//
                CwSalary cwSalary = iCwSalaryService.selectOne(wrapper2);
                cwSalary.setStates(4);
                iCwSalaryService.updateById(cwSalary);
                return (ResponseBean) responseSuccess("已审批");
            }
            if (confirm == 5) {
                wrapper2.eq("id", id).eq("states", 2);
                CwSalary cwSalary = iCwSalaryService.selectOne(wrapper2);
                cwSalary.setStates(5);
                iCwSalaryService.updateById(cwSalary);
                return (ResponseBean) responseSuccess("审批不通过");
            }
        } else if (empDeptVo.getLevel() == 2) {
            wrapper2.eq("id", id).eq("states", 4);
            CwSalary cwSalary = iCwSalaryService.selectOne(wrapper2);
            cwSalary.setStates(6);
            iCwSalaryService.updateById(cwSalary);
            CwCapitals capitals = new CwCapitals();
            capitals.setCptname(cwSalary.getEmpid());//收支名称
            capitals.setCptdate(new Date());//收支日期
            capitals.setCpttype("123");//存取账户
            capitals.setTypec("工资");
            capitals.setCptacount(new BigDecimal(6000));
            capitals.setFlag(0);
            capitals.setCrer(empDeptVo.getId());
            capitals.setState(new BigDecimal(1));
            capitals.setIsdelete(new BigDecimal(1));
            iCwCapitalsService.insert(capitals);
            return (ResponseBean) responseSuccess("出纳发放");
        }
        return (ResponseBean) responseFail("操作失败");
    }

    /**
     * 10.3 工资统计分析      （需要移动API）
     * 工资总额，，        cw_salary所有部门工资     图表展示   select sum(),sum(),sum()
     */
    @GetMapping("Totalwages")
    @ApiOperation(value = "工资总额")
    @ApiImplicitParam(name = "month", paramType = "query", dataType = "String", required = true, value = "月份")
    public ResponseBean Totalwages(String month) {
        Wrapper wrapper = Condition.create().like("wagemonth",month);
        Float aFloat = iCwSalaryService.selectTotalwages(wrapper);
        System.out.println(aFloat);
        return (ResponseBean) responseSuccess(aFloat);
    }

    /**
     * 部门总额
     */
    @GetMapping("Sectortotal")
    @ApiOperation(value = "部门总额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "did", paramType = "query", dataType = "String", required = true, value = "部门id"),
            @ApiImplicitParam(name = "month", paramType = "query", dataType = "String", required = true, value = "月份")
    })
    public ResponseBean<List<SalaryAttVo>> Sectortotal(String did, String month) {
        Wrapper wrapper = Condition.create().eq("d.id",did).like("cw.wagemonth",month);
        List<CwSalary> list = iCwSalaryService.selectSectortotal(wrapper);
        return (ResponseBean<List<SalaryAttVo>>) responseSuccess(list);
    }

    /**
     * 公司社保总额
     */
    @GetMapping("Totalsocial")
    @ApiOperation(value = "公司社保总额")
    @ApiImplicitParam(name = "month", paramType = "query", dataType = "String", required = true, value = "月份")
    public ResponseBean Totalsocial(String month) {
        Wrapper wrapper = Condition.create().like("wagemonth",month);
        Float bigDecimal = iCwSalaryService.selectTotalsocial(wrapper);
        return (ResponseBean) responseSuccess(bigDecimal);
    }
}
